//
//  LinkStack.cpp
//  DataStructure
//
//  Created by 舒磊明 on 2020/6/26.
//  Copyright © 2020 Shuleiming. All rights reserved.
//

#include "LinkStack.hpp"

// 初始化
void InitLinkStack(LinkStack &S)
{
    S = (LinkNode*)malloc(sizeof(LinkStack));
    S->next = nullptr;
}

// 入栈
bool Push(LinkStack &S, int x)
{
    LinkNode *s = (LinkNode*)malloc(sizeof(LinkNode));
    s->data = x;
    s->next = S->next;
    S->next = s;
    return true;
}

// 出栈
bool Pop(LinkStack &S, int &x)
{
    if (S->next == nullptr) {
        return false;
    }
    LinkNode *p = S->next;
    S->next = p->next;
    x = p->data;
    free(p);
    return true;
}

// 获取栈顶元素
bool GetTop(LinkStack &S, int &x)
{
    LinkNode *p = S->next;
    if (p == nullptr) {
        return false;
    }
    x = p->data;
    return true;
}

// 判定栈空
bool StackEmpty(LinkStack S)
{
    return S->next == nullptr;
}
